/*
 * Copyright (c) 2017. 深圳一七网络科技. All rights reserved.
 */

package com.fenxiangle.yueding.feature.publish.presenter;

import android.text.TextUtils;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.fenxiangle.yueding.entity.bo.FoucsBo;
import com.fenxiangle.yueding.entity.bo.FoucsListBo;
import com.fenxiangle.yueding.entity.bo.OrderBo;
import com.fenxiangle.yueding.entity.bo.PageBo;
import com.fenxiangle.yueding.entity.bo.PublishBo;
import com.fenxiangle.yueding.entity.bo.PublishDemandBo;
import com.fenxiangle.yueding.entity.bo.UserCenterBo;
import com.fenxiangle.yueding.feature.mine.dependencies.mine.DaggerMinePresenterComponent;
import com.fenxiangle.yueding.feature.publish.contract.PublishContract;
import com.fenxiangle.yueding.feature.publish.dependencies.mine.DaggerPublishPresenterComponent;
import com.suozhang.framework.component.http.ApiError;
import com.suozhang.framework.component.http.ex.ApiException;
import com.suozhang.framework.component.http.ex.ServerException;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.DateUtil;
import com.suozhang.framework.utils.T;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class PublishPresenter implements PublishContract.Presenter {

    private PublishContract.View mView;


    @Inject

    PublishContract.Model mModel;


    public PublishPresenter(PublishContract.View mView) {
        this.mView = mView;
        DaggerPublishPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void publishDemand(int type, String occupation, String describe, String numPeople, String startTime, String endTime,
                              PoiItem poiItem, String unitPrice, String totalPrice, int credit) {
        PublishBo publishBo = new PublishBo();
        if (TextUtils.isEmpty(occupation)) {
            mView.showError("请选择职业！");
            return;
        }
        if (TextUtils.isEmpty(describe)) {
            mView.showError("请输入需求描述！");
            return;
        }
        if (TextUtils.isEmpty(startTime)) {
            mView.showError("请选择开始时间！");
            return;
        }
        if (TextUtils.isEmpty(startTime)) {
            mView.showError("请选择结束时间！");
            return;
        }
        if (DateUtil.getTimeCompareSize(startTime,
                endTime) != 3) {
            mView.showError("结束时间必须大于开始时间！");
            return;
        }
        if (poiItem == null) {
            mView.showError("请选择约定地点！");
            return;
        }

        if (TextUtils.isEmpty(numPeople)) {
            mView.showError("请输入需求人数！");
            return;
        }
        if (Integer.valueOf(numPeople) <= 0) {
            mView.showError("需求人数不能小于￥0！");
            return;
        }
        if (TextUtils.isEmpty(unitPrice)) {
            mView.showError("请输入每个人支付的费用！");
            return;
        }

        if (Integer.valueOf(unitPrice) <= 0) {
            mView.showError("每个人支付的费用不能小于￥0！");
            return;
        }

//        if (TextUtils.isEmpty(totalPrice)) {
//            mView.showError("请输入总共支付的费用！");
//            return;
//        }
//        if (Integer.valueOf(totalPrice) <= 0) {
//            mView.showError("总共支付的费用不能小于￥0！");
//            return;
//        }

        publishBo.setAgreedPlace(poiItem.getTitle() + "(" + poiItem.getSnippet() + ")");
        publishBo.setCoordinate(poiItem.getLatLonPoint().getLatitude() + "," + poiItem.getLatLonPoint().getLongitude());
        publishBo.setDescribe(describe);
        publishBo.setStartTime(startTime);
        publishBo.setEndTime(endTime);
        publishBo.setNumPeople(Integer.valueOf(numPeople));
        publishBo.setType(type);
        publishBo.setOccupation(occupation);
        publishBo.setUnitPrice(Integer.valueOf(unitPrice));
//        publishBo.setTotalPrice(Integer.valueOf(totalPrice));
        mModel.publishDemand(publishBo).compose(mView.bindToLife()).subscribe(new Observer<OrderBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(OrderBo s) {
                mView.dismissLoading();
                mView.showPublishSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                ApiException exception = (ApiException) e;
                ApiError error = ApiError.codeOf(exception.code());
                if (error == ApiError.Insufficient_Balance) {
                    mView.showInsufficientBalance();
                } else {
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void publishInvite(int type,
                              String invitationUserUid,
                              String describe,
                              String startTime,
                              String endTime,
                              String agreedPlace,
                              String coordinate,
                              String totalPrice) {
        PublishBo publishBo = new PublishBo();

        if (TextUtils.isEmpty(describe)) {
            mView.showError("请输入需求描述！");
            return;
        }
        if (TextUtils.isEmpty(startTime)) {
            mView.showError("请选择开始时间！");
            return;
        }
        if (TextUtils.isEmpty(startTime)) {
            mView.showError("请选择结束时间！");
            return;
        }

        if (TextUtils.isEmpty(agreedPlace) || TextUtils.isEmpty(coordinate)) {
            mView.showError("请选择约定地点！");
            return;
        }

        if (TextUtils.isEmpty(totalPrice)) {
            mView.showError("请输入总共支付的费用！");
            return;
        }
        if (Integer.valueOf(totalPrice) <= 0) {
            mView.showError("总共支付的费用不能小于￥0！");
            return;
        }


        publishBo.setAgreedPlace(agreedPlace);
        publishBo.setCoordinate(coordinate);
        publishBo.setDescribe(describe);
        publishBo.setStartTime(startTime);
        publishBo.setEndTime(endTime);
        publishBo.setType(type);
        publishBo.setInvitationUserUid(invitationUserUid);
        publishBo.setTotalPrice(Integer.valueOf(totalPrice));

        mModel.publishInvite(publishBo).compose(mView.bindToLife()).subscribe(new Observer<OrderBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(OrderBo s) {
                mView.dismissLoading();
                mView.showPublishSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                ApiException exception = (ApiException) e;
                ApiError error = ApiError.codeOf(exception.code());
                if (error == ApiError.Insufficient_Balance) {
                    mView.showInsufficientBalance();
                } else {
                    mView.showError(e.getMessage());
                }
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });

    }

    @Override
    public void getPublishDemandList(int pageNow, int pageSize) {
        mModel.getPublishDemandList(new PageBo(pageNow, pageSize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<PublishDemandBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<PublishDemandBo> couponBoPageData) {
                if (couponBoPageData != null && couponBoPageData.size() > 0) {
                    mView.showPublishListSuccess(couponBoPageData);
                } else {
                    mView.showPublishListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getPublishDemandListMore(int pageNow, int pageSize) {
        mModel.getPublishDemandList(new PageBo(pageNow, pageSize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<PublishDemandBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<PublishDemandBo> couponBoPageData) {
                mView.showPublishListMoreSuccess(couponBoPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListMoreError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getPublishInviteList(int pageNow, int pageSize, Integer type) {
        mModel.getPublishInviteList(new PageBo(pageNow, pageSize, type))
                .compose(mView.bindToLife()).subscribe(new Observer<List<PublishDemandBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<PublishDemandBo> couponBoPageData) {
                if (couponBoPageData != null && couponBoPageData.size() > 0) {
                    mView.showPublishListSuccess(couponBoPageData);
                } else {
                    mView.showPublishListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getPublishInviteListMore(int pageNow, int pageSize, Integer type) {
        mModel.getPublishInviteList(new PageBo(pageNow, pageSize, type))
                .compose(mView.bindToLife()).subscribe(new Observer<List<PublishDemandBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<PublishDemandBo> couponBoPageData) {
                mView.showPublishListMoreSuccess(couponBoPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showPublishListMoreError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void cancleInvitedOrder(String orderId,String type) {
        mModel.cancleInvitedOrder(new OrderBo(orderId,type)).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showcancleOrderSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());

                if (e instanceof ServerException) {

                    //ServerException 约定异常 100x 200x 300x等
                    // NOTE: 2017/5/23 直接显示服务端返回的msg
                    ServerException exception = (ServerException) e;
                    ApiError error = ApiError.codeOf(exception.code());
                    T.showShort(error.code() + "");
//                    if (error == ApiError.UNAUTHORIZED || error == ApiError.REFRESH_TOKEN_EXPIRE) {
//
//                    }
                }
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void cancleConfrimOrder(String orderId,String acceptUserUid) {
        mModel.confrimInvitedOrder(new OrderBo(orderId,acceptUserUid,"")).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showConfrimOrderSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void txOrder(String orderId) {
        mModel.txOrder(new OrderBo(orderId)).compose(mView.bindToLife()).subscribe(new Observer<PublishBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(PublishBo s) {
                mView.dismissLoading();
                mView.showConfrimTxSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void txWeiquan(String orderId, String defendant) {
        OrderBo orderBo = new OrderBo();
        orderBo.setOrderId(orderId);
        orderBo.setDefendant(defendant);

        mModel.txWeiquan(orderBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showWeiQunanOrderSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
